#include <copydir.h>
//将文件夹A复制到B
void backup_directory(char *path1, char *path2, struct file_rate *filerate) {
	DIR *dir = opendir(path1);
	if (access(path2,F_OK) == -1) {
		mkdir_file(path2);
	}
	struct dirent *entry;
	char pathname1[CH_SIZE],pathname2[CH_SIZE];
	char ch[CH_SIZE];
	struct stat buf;
	
	if(dir != NULL) {
		while(entry = readdir(dir)) {
			if(!strcmp(".",entry->d_name)||!strcmp("..",entry->d_name))
				continue;
			sprintf(ch, "%s/%s", path1, entry->d_name);
			stat(ch,&buf);
			sprintf(pathname1,"%s/%s", path1,entry->d_name);
			sprintf(pathname2,"%s/%s", path2,entry->d_name);
			//printf("%s %s %s\n",ch,pathname1,pathname2);
			switch(buf.st_mode & S_IFMT) {
				case S_IFBLK:		break;
				case S_IFCHR:		break;
				case S_IFDIR:	
					mkdir_file(pathname2);
					backup_directory(pathname1, pathname2, filerate);
					break;
				case S_IFIFO:	
					mkfifo(pathname2, 0666);
					break;
				case S_IFLNK:		break;
				case S_IFREG:	
					copy_file(pathname1,pathname2);
					break;
				case S_IFSOCK:		break;
				default:		break;
			}
			filerate->sum++;
			pthread_t ptid;
			pthread_create(&ptid, NULL, putout_rate, filerate);
       	 	pthread_join(ptid,NULL);
		}
	}
	closedir(dir);
	free(entry);
	
	FILE *f = fopen("./pro_copy.log","a+");
	time_t rawtime;
	struct tm *timeinfo;
	timeinfo = localtime(&rawtime);
	fprintf(f,"\007The current date/time is:%s\n",asctime(timeinfo));
	fprintf(f,"backup directory %s --> %s\n",pathname1, pathname2);
	fclose(f);
}
